package GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import Applikationslogik.Person;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

/**
 *
 * @author Rene & Daniel
 */
public class Login extends JDialog {

        private final JPanel contentPanel = new JPanel();
        private JComboBox comboBox_Benutzer;
        JButton button_OK;
        Person loginuser;


        /**
         * Login Fenster wird gebaut
         */
        public Login() {
                setModal(true);
                setTitle("LOGIN");
                setBounds(100, 100, 352, 196);
                getContentPane().setLayout(null);
                contentPanel.setBounds(0, 0, 336, 158);
                contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
                getContentPane().add(contentPanel);
                contentPanel.setLayout(null);

                comboBox_Benutzer = new JComboBox();
                comboBox_Benutzer.setFont(new Font("Tahoma", Font.PLAIN, 12));
                comboBox_Benutzer.setBounds(30, 56, 150, 20);
                contentPanel.add(comboBox_Benutzer);

                JLabel label_BenutzerWaehlen = new JLabel("Benutzer w\u00E4hlen:");
                label_BenutzerWaehlen.setFont(new Font("Tahoma", Font.PLAIN, 12));
                label_BenutzerWaehlen.setBounds(30, 25, 118, 20);
                contentPanel.add(label_BenutzerWaehlen);
                button_OK = new JButton("OK");
                button_OK.setBounds(249, 110, 64, 23);
                contentPanel.add(button_OK);
                button_OK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                SwingUtilities.getWindowAncestor(button_OK).dispose();
                        }
                });
                button_OK.setActionCommand("OK");
                getRootPane().setDefaultButton(button_OK);
        }

        /**
         * der ComboBox werden die Personen übergeben 
         * @param person
         * @return
         */
        public Person ermittleLoginUser(HashMap<String, Person> person)
        {
                // Userliste in Auswahlliste eintragen
                for (String key : person.keySet()) {
                   String s = key;
                   comboBox_Benutzer.addItem(s);
                }

                this.setVisible(true);

                loginuser = (Person) person.get(comboBox_Benutzer.getSelectedItem());
                return loginuser;
        }
}